<template>
  <div class="daily_TrainStation-container">
    <div class="header">
      <h1 class="title">每日过站管理</h1>
      <p class="subtitle">每日过站信息管理，包括id、日期、车次编号、站序、站名、站名拼音、进站时间、出站时间、停站时长、里程（公里）、新增时间、修改时间</p>
    </div>

    <a-card class="operation-card">
      <div class="operation-bar">
        <a-space>
          <a-date-picker v-model:value="params.date" valueFormat="YYYY-MM-DD" placeholder="请选择日期" />
          <train-select-view v-model="params.trainCode" width="200px"></train-select-view>
          <a-button type="primary" @click="handleQuery()" class="refresh-btn">
            <template #icon><search-outlined /></template>
            查找
          </a-button>
          <a-button type="primary" @click="onAdd" class="add-btn">
            <template #icon><plus-outlined /></template>
            新增每日车站
          </a-button>
        </a-space>
        <div class="statistics">
          <div class="stat-item">
            <dashboard-outlined />
            <span>每日车站总数: {{ pagination.total }}</span>
          </div>
        </div>
      </div>
    </a-card>

    <a-card class="table-card">
      <a-table
        :dataSource="daily_TrainStations"
        :columns="columns"
        :pagination="pagination"
        @change="handleTableChange"
        :loading="loading"
        :locale="{ emptyText: '暂无每日车站数据' }"
        rowKey="id"
        bordered
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'operation'">
            <a-space class="operation-buttons">
              <a-tooltip title="编辑每日车站信息">
                <a-button type="primary" size="small" @click="onEdit(record)" class="edit-btn">
                  <template #icon><edit-outlined /></template>
                </a-button>
              </a-tooltip>
              <a-popconfirm
                title="确定要删除此每日车站吗？"
                ok-text="确认"
                cancel-text="取消"
                @confirm="onDelete(record)"
              >
                <a-tooltip title="删除每日车站">
                  <a-button type="primary" danger size="small" class="delete-btn">
                    <template #icon><delete-outlined /></template>
                  </a-button>
                </a-tooltip>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </a-table>
    </a-card>

    <a-modal
      v-model:visible="visible"
      :title="daily_TrainStation.id ? '编辑每日车站' : '添加新每日车站'"
      @ok="handleOk"
      ok-text="确认"
      cancel-text="取消"
      :width="600"
      centered
      class="daily_TrainStation-modal"
    >
      <a-form :model="daily_TrainStation" :label-col="{span: 6}" :wrapper-col="{ span: 16 }">
        <a-form-item label="日期">
          <a-date-picker v-model:value="daily_TrainStation.date" valueFormat="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%" />
        </a-form-item>
        <a-form-item label="车次编号">
          <train-select-view v-model="daily_TrainStation.trainCode" width="50%"></train-select-view>
        </a-form-item>
        <a-form-item label="站序">
          <a-input v-model:value="daily_TrainStation.index" placeholder="请输入站序" allow-clear />
        </a-form-item>
        <a-form-item label="站名">
          <a-input v-model:value="daily_TrainStation.name" placeholder="请输入站名" allow-clear />
        </a-form-item>
        <a-form-item label="站名拼音">
          <a-input v-model:value="daily_TrainStation.namePinyin" disabled placeholder="请输入站名拼音" allow-clear />
        </a-form-item>
        <a-form-item label="进站时间">
          <a-time-picker v-model:value="daily_TrainStation.inTime" valueFormat="HH:mm:ss" placeholder="请选择时间" style="width: 100%" />
        </a-form-item>
        <a-form-item label="出站时间">
          <a-time-picker v-model:value="daily_TrainStation.outTime" valueFormat="HH:mm:ss" placeholder="请选择时间" style="width: 100%" />
        </a-form-item>
        <a-form-item label="停站时长">
          <a-time-picker v-model:value="daily_TrainStation.stopTime" disabled valueFormat="HH:mm:ss" placeholder="请选择时间" style="width: 100%" />
        </a-form-item>
        <a-form-item label="里程（公里）">
          <a-input v-model:value="daily_TrainStation.km" placeholder="请输入里程（公里）" allow-clear />
        </a-form-item>
      </a-form>
      <template #footer>
        <a-button key="back" @click="visible = false">取消</a-button>
        <a-button key="submit" type="primary" @click="handleOk">提交</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
import {defineComponent, ref, onMounted, watch} from 'vue';
import { notification } from "ant-design-vue";
import axios from "axios";
import {
  PlusOutlined, EditOutlined,
  DeleteOutlined, DashboardOutlined,SearchOutlined
} from '@ant-design/icons-vue';
import TrainSelectView from "@/components/train-select.vue";
import {pinyin} from "pinyin-pro";
import dayjs from "dayjs";

export default defineComponent({
  name: "daily_TrainStation-view",
  components: {
    TrainSelectView,
    PlusOutlined, EditOutlined, DeleteOutlined,
    DashboardOutlined,SearchOutlined
  },
  setup() {
    const visible = ref(false);
    let daily_TrainStation = ref({
      id: undefined,
      date: undefined,
      trainCode: undefined,
      index: undefined,
      name: undefined,
      namePinyin: undefined,
      inTime: undefined,
      outTime: undefined,
      stopTime: undefined,
      km: undefined,
      createTime: undefined,
      updateTime: undefined
    });
    const daily_TrainStations = ref([]);
    const pagination = ref({
      total: 0,
      current: 1,
      pageSize: 10,
    });
    let loading = ref(false);
    const columns = [
      {
        title: '日期',
        dataIndex: 'date',
        key: 'date',
      },
      {
        title: '车次编号',
        dataIndex: 'trainCode',
        key: 'trainCode',
      },
      {
        title: '站序',
        dataIndex: 'index',
        key: 'index',
      },
      {
        title: '站名',
        dataIndex: 'name',
        key: 'name',
      },
      {
        title: '站名拼音',
        dataIndex: 'namePinyin',
        key: 'namePinyin',
      },
      {
        title: '进站时间',
        dataIndex: 'inTime',
        key: 'inTime',
      },
      {
        title: '出站时间',
        dataIndex: 'outTime',
        key: 'outTime',
      },
      {
        title: '停站时长',
        dataIndex: 'stopTime',
        key: 'stopTime',
      },
      {
        title: '里程（公里）',
        dataIndex: 'km',
        key: 'km',
      },
      {
        title: '操作',
        dataIndex: 'operation',
        align: 'center',
        width: '150px'
      }
    ];

    let params=ref({
      trainCode: null,
      date:null
    });
    watch(() => daily_TrainStation.value.name, ()=>{
      if (Tool.isNotEmpty(daily_TrainStation.value.name)) {
        daily_TrainStation.value.namePinyin = pinyin(daily_TrainStation.value.name, { toneType: 'none'}).replaceAll(" ", "");
      } else {
        daily_TrainStation.value.namePinyin = "";
      }
    }, {immediate: true});
    // 自动计算停车时长
    watch(() => daily_TrainStation.value.inTime, ()=>{
      let diff = dayjs(daily_TrainStation.value.outTime, 'HH:mm:ss').diff(dayjs(daily_TrainStation.value.inTime, 'HH:mm:ss'), 'seconds');
      daily_TrainStation.value.stopTime = dayjs('00:00:00', 'HH:mm:ss').second(diff).format('HH:mm:ss');
    }, {immediate: true});

    // 自动计算停车时长
    watch(() => daily_TrainStation.value.outTime, ()=>{
      let diff = dayjs(daily_TrainStation.value.outTime, 'HH:mm:ss').diff(dayjs(daily_TrainStation.value.inTime, 'HH:mm:ss'), 'seconds');
      daily_TrainStation.value.stopTime = dayjs('00:00:00', 'HH:mm:ss').second(diff).format('HH:mm:ss');
    }, {immediate: true});
    const onAdd = () => {
      daily_TrainStation.value = {};
      visible.value = true;
    };

    const onEdit = (record) => {
      daily_TrainStation.value = window.Tool.copy(record);
      visible.value = true;
    };

    const onDelete = (record) => {
      axios.delete("/business/admin/daily_TrainStation/delete/" + record.id).then((response) => {
        const data = response.data;
        if (data.success) {
          notification.success({
            message: '删除成功',
          });
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize,
          });
        } else {
          notification.error({description: data.message});
        }
      });
    };

    const handleOk = () => {
      axios.post("/business/admin/daily_TrainStation/save", daily_TrainStation.value).then((response) => {
        let data = response.data;
        if (data.success) {
          notification.success({
            message: '保存成功',
            description: daily_TrainStation.value.id ? '每日车站信息已更新' : '新每日车站已添加',
          });
          visible.value = false;
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          });
        } else {
          notification.error({description: data.message});
        }
      });
    };

    const handleQuery = (param) => {
      if (!param) {
        param = {
          page: 1,
          size: pagination.value.pageSize
        };
      }
      loading.value = true;
      axios.get("/business/admin/daily_TrainStation/query-list", {
        params: {
          page: param.page,
          size: param.size,
          trainCode:params.value.trainCode,
          date:params.value.date
        }
      }).then((response) => {
        loading.value = false;
        let data = response.data;
        if (data.success) {
          daily_TrainStations.value = data.content.list;
          pagination.value.current = param.page;
          pagination.value.total = data.content.total;
        } else {
          notification.error({description: data.message});
        }
      }).catch(() => {
        loading.value = false;
      });
    };

    const handleTableChange = (pagination) => {
      handleQuery({
        page: pagination.current,
        size: pagination.pageSize
      });
    };

    onMounted(() => {
      handleQuery({
        page: 1,
        size: pagination.value.pageSize
      });
    });

    return {
      daily_TrainStation,
      visible,
      daily_TrainStations,
      pagination,
      columns,
      handleTableChange,
      handleQuery,
      loading,
      onAdd,
      handleOk,
      onEdit,
      onDelete,
      params
    };
  },
});
</script>

<style scoped>
.daily_TrainStation-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
}

.header {
  margin-bottom: 24px;
  text-align: center;
}

.title {
  font-size: 28px;
  font-weight: 600;
  color: #1f2f3d;
  margin-bottom: 8px;
  background: linear-gradient(90deg, #1890ff, #722ed1);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.subtitle {
  color: #606266;
  font-size: 16px;
}

.operation-card {
  margin-bottom: 24px;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.table-card {
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.operation-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.refresh-btn, .add-btn {
  border-radius: 6px;
  font-weight: 500;
  padding: 0 20px;
  height: 36px;
}

.add-btn {
  background: linear-gradient(90deg, #13c2c2, #1890ff);
  border: none;
}

.statistics {
  display: flex;
  gap: 20px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #f0f9ff;
  padding: 8px 16px;
  border-radius: 6px;
  font-weight: 500;
  color: #1890ff;
}

.operation-buttons {
  display: flex;
  justify-content: center;
}

.edit-btn {
  background: linear-gradient(90deg, #52c41a, #389e0d);
  border: none;
}

.delete-btn {
  border: none;
}

:deep(.ant-table-thead > tr > th) {
  background-color: #f0f9ff !important;
  font-weight: 600;
  color: #1f2f3d;
}

:deep(.ant-table-row) {
  transition: background-color 0.3s;
}

:deep(.ant-table-row:hover) {
  background-color: #f0f9ff !important;
}

.daily_TrainStation-modal :deep(.ant-modal-header) {
  border-radius: 10px 10px 0 0;
}

.daily_TrainStation-modal :deep(.ant-modal-body) {
  padding: 24px;
}

.daily_TrainStation-modal :deep(.ant-form-item) {
  margin-bottom: 20px;
}

.daily_TrainStation-modal :deep(.ant-input) {
  border-radius: 6px;
  height: 40px;
}

@media (max-width: 768px) {
  .operation-bar {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }

  .daily_TrainStation-container {
    padding: 15px;
  }

  .title {
    font-size: 24px;
  }

  .statistics {
    width: 100%;
    justify-content: flex-start;
  }
}
</style>